#include <iostream>
#include <string>
#include <vector>

// 计算一个数字字符串的库存
std::string getInventory(const std::string &num)
{
  int digitCount[10] = {0};
  for (std::string::size_type i = 0; i < num.length(); ++i)
  {
    digitCount[num[i] - '0']++;
  }

  std::string inventory;
  for (int i = 0; i < 10; ++i)
  {
    if (digitCount[i] > 0)
    {
      char countStr[20];
      sprintf(countStr, "%d", digitCount[i]);
      inventory += countStr;
      char digitStr[2];
      sprintf(digitStr, "%d", i);
      inventory += digitStr;
    }
  }
  return inventory;
}

// 判断数字字符串的类型
void classifyNumber(const std::string &num)
{
  std::vector<std::string> inventoryHistory;
  inventoryHistory.push_back(num);

  for (int step = 1; step <= 15; ++step)
  {
    std::string currentInventory = getInventory(inventoryHistory.back());

    // 检查是否是自库存数
    if (currentInventory == inventoryHistory.back())
    {
      if (step == 1)
      {
        std::cout << num << " is self-inventorying" << std::endl;
      }
      else
      {
        std::cout << num << " is self-inventorying after " << step - 1 << " steps" << std::endl;
      }
      return;
    }

    // 检查是否进入库存循环
    for (std::vector<std::string>::size_type j = 0; j < inventoryHistory.size(); ++j)
    {
      if (currentInventory == inventoryHistory[j])
      {
        int loopLength = inventoryHistory.size() - j;
        std::cout << num << " enters an inventory loop of length " << loopLength << std::endl;
        return;
      }
    }

    inventoryHistory.push_back(currentInventory);
  }

  // 15 次迭代后仍未分类
  std::cout << num << " can not be classified after 15 iterations" << std::endl;
}

int main()
{
  std::string num;
  while (std::cin >> num)
  {
    if (num == "-1")
    {
      break;
    }
    classifyNumber(num);
  }
  return 0;
}